package com.itheima.health.controller;

import com.itheima.health.entity.Result;
import com.itheima.health.exception.HealthException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Author tang.qingbo
 * @Date 2020/7/27 20:33
 * @Version 1.0
 */

/**
 * 全局异常处理
 */
@RestControllerAdvice//相当于在调用controller方法前加个try
public class HealthExceptionHandler {

    //HealthException异常
    @ExceptionHandler(HealthException.class)//相当于异常的catch
    public Result handleHealthException(HealthException he) {
        return new Result(false, he.getMessage());
    }

    //最大异常
    @ExceptionHandler(Exception.class)
    public Result handleException(Exception e) {
        //打印异常信息
        e.printStackTrace();
        return new Result(false, "操作失败,发生未知异常,请联系管理员");
    }
}
